﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using HomeDmsUtils;

namespace HomeDms.Imaging
{
    public partial class SimpleImageEditor : UserControl
    {
        public SimpleImageEditor()
        {
            InitializeComponent();
        }
        
        public Image Image
        {
            get
            {
                return pictureBox.OriginalImage;
            }
            set
            {
                pictureBox.OriginalImage = value;
            }
        }

        private ImageToolPanel currentTool = null;

        private void SetupTool(object caller, ImageToolPanel pan)
        {
            if (currentTool != null) // we're in one of tools now
            {
                if (currentTool.Modified)
                {
                    switch (Localizer.MsgBox(this, "Save current image?", "Image Editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
                    {
                        case DialogResult.Yes:
                            RemoveTool();
                            break;
                        case DialogResult.No:
                            pictureBox.OriginalImage = currentTool.Original;
                            RemoveTool();
                            break;
                        case DialogResult.Cancel:
                            return;
                    }
                }
                else
                    RemoveTool();
            }
            (caller as ToolStripButton).Checked = true;
            currentTool = pan;
            pan.Dock = DockStyle.Fill;
            pan.PictureBox = pictureBox;
            pan.CancelTool += new EventHandler(pan_CancelTool);
            pan.ApplyTool += new EventHandler(pan_ApplyTool);
            panel1.Controls.Add(pan);
        }

        public event EventHandler ImageChanged;

        void pan_ApplyTool(object sender, EventArgs e)
        {
            RemoveTool();
            if (ImageChanged != null)
                ImageChanged(this, EventArgs.Empty);
        }

        public void RemoveTool()
        {
            foreach (ToolStripButton tb in toolStrip1.Items)
                tb.Checked = false;
            panel1.Controls.Remove(currentTool);
            currentTool = null;
        }

        void pan_CancelTool(object sender, EventArgs e)
        {            
            RemoveTool();
        }

        void pan_ImageInvalidated(object sender, EventArgs e)
        {
            //pictureBox1.Image = (sender as ImageToolPanel).Image;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ImageToolPanel pan = new BrightnessContrastToolPanel();
            SetupTool(sender, pan);
        }

        private void tbRotate_Click(object sender, EventArgs e)
        {
            ImageToolPanel pan = new RotateToolPanel();
            SetupTool(sender, pan);
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            ImageToolPanel pan = new FlipVerticalToolPanel();
            SetupTool(sender, pan);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            ImageToolPanel pan = new SharpenToolPanel();
            SetupTool(sender, pan);
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            ImageToolPanel pan = new CropToolPanel();
            SetupTool(sender, pan);
        }

        private void panel1_ControlAdded(object sender, ControlEventArgs e)
        {
            if (currentTool != null)
                label1.Visible = false;
        }

        private void panel1_ControlRemoved(object sender, ControlEventArgs e)
        {
            label1.Visible = true;
        }

    }
}
